/*
    4. Write a program in C to insert a new node at the beginning of a Singly Linked List. 
    Test Data and Expected Output :

    Input the number of nodes : 3                                                                                
    Input data for node 1 : 5                                                                                    
    Input data for node 2 : 6                                                                                    
    Input data for node 3 : 7                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    Data = 5                                                                                                     
    Data = 6                                                                                                     
    Data = 7                                                                                                     
                                                                                                                
    Input data to insert at the beginning of the list : 4                                                        
                                                                                                                
    Data after inserted in the list are :                                                                        
    Data = 4                                                                                                     
    Data = 5                                                                                                     
    Data = 6                                                                                                     
    Data = 7  
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List next;
} Node;

List createList(int n)
{
    int i;
    List newNode = NULL;
    List root = NULL;
    List curNode = NULL;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL)
            return NULL;
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        newNode->next = NULL;
        if(root == NULL){
            root = newNode;
            curNode = newNode;
        }else{
            curNode->next = newNode;
            curNode = newNode;
        }
    }

    return root;
}

List insertNodeAtBegin(List root, int x)
{
    List newNode = (List)malloc(sizeof(Node));
    newNode->next = root;
    newNode->x = x;
    return newNode;
}

void printList(List list)
{
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }
    printf("\nData entered in list are :\n");
    while(list != NULL){
        printf("Data = %d\n", list->x);
        list = list->next;
    }
    printf("\n");
}

int main(void)
{
    List root = NULL;
    int n, x;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createList(n);
    printList(root);
    printf("Input data to insert at the beginning of the list : ");
    scanf("%d", &x);
    root = insertNodeAtBegin(root, x);
    printList(root);

    return 0;
}